package com.ctms.system.controller;

import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.ExportService;
import com.ctms.system.service.StudentScoreService;
import com.ctms.system.service.StudentService;
import com.ctms.system.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/export")
@Controller
public class ExportController {

    @Autowired
    ExportService exportService;
    @Autowired
    StudentService studentService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentScoreService studentScoreService;


    @RequestMapping(value = "/exportExcel")
    @ResponseBody
    public ResponseEntity<byte[]> exportExcel(Integer type,String condition) {
        //按姓名、学号搜索全部导出,班级按条件
        if (type == 1 || type == 2) {
            //查询所有的学生信息
            List<UserDO> userDOS = studentService.getAllStudent();
            //调用exportService的exportExcel导出方法
            return exportService.exportExcel(userDOS);
        } else {
            //按班级查询学生信息
            List<UserDO> userDOS = studentService.getStudentByClass(condition);
            //调用exportService的exportExcelByClass导出方法
            return exportService.exportExcelByClass(userDOS);
        }
    }

    //教师导出我的学生
    @RequestMapping("/exportMyStu")
    @ResponseBody
    public ResponseEntity<byte[]> exportMyStu(Integer type,String condition) {
        //获取登录人ID
        UserDO userDO = ShiroUtils.getUser();
        int userId = Integer.parseInt(Long.toString(userDO.getUserId()));
        //按姓名、学号搜索全部导出,班级按条件,课程按条件
        if (type == 1 || type == 2) {
            List<UserDO> userDOS = teacherService.exportMyStu(userId);
            return exportService.exportMyStu(userDOS);
        } else if (type == 3){
            //按班级
            List<UserDO> userDOS = teacherService.exportMyStuByClass(userId,condition);
            return exportService.exportMyStuByClass(userDOS);
        }else {
            //按课程
            List<UserDO> userDOS = teacherService.exportMyStuByCourse(userId,condition);
            return exportService.exportMyStuByCourse(userDOS);
        }
    }

    //管理员导出
    @RequestMapping(value = "/exportStuScore")
    @ResponseBody
    public ResponseEntity<byte[]> exportStuScore(Integer type,String condition) {
        //按姓名、学号搜索全部导出,班级、课程按条件
        if (type == 1 || type == 2) {
            List<UserDO> userDOS = studentScoreService.exportScore();
            return exportService.exportScore(userDOS);
        } else if (type == 3){
            //课程
            List<UserDO> userDOS = studentScoreService.exportScoreByCourse(condition);
            return exportService.exportScoreByCourse(userDOS);
        }else {
            //班级
            List<UserDO> userDOS = studentScoreService.exportScoreByClass(condition);
            return exportService.exportScoreByClass(userDOS);
        }
    }

}
